package org.finesys.system.controller;


import java.util.Arrays;
import java.util.List;

import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.module.R;
import org.finesys.common.excel.annotaion.RequestExcel;
import org.finesys.common.excel.annotaion.ResponseExcel;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
import org.finesys.system.api.entity.SysPost;
import org.finesys.system.api.vo.PostExcelVO;
import org.finesys.system.constants.ControllerRestApiConfig;
import org.finesys.system.service.SysPostService;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;


@RestController
@RequiredArgsConstructor
@RequestMapping(ControllerRestApiConfig.POST)
@Tag(name = "岗位管理模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysPostController {

    private final SysPostService sysPostService;


    /**
     * 新增岗位信息表
     *
     * @param sysPost 岗位信息表
     * @return R
     */
    @Operation(summary = "新增岗位信息", description = "新增岗位信息表")
    @LogAnnotation(title = "新增岗位信息", operateType = OperateType.INSERT)
    @PostMapping(value = RestApiConfig.ADD)
    @PreAuthorize("@pms.hasPermission('sys_post_add')")
    public R<?> save(@RequestBody SysPost sysPost) {
        return R.success(sysPostService.save(sysPost));
    }

    /**
     * 修改岗位信息表
     *
     * @param sysPost 岗位信息表
     * @return R
     */
    @Operation(summary = "修改岗位信息表", description = "修改岗位信息表")
    @LogAnnotation(title = "修改岗位信息表", operateType = OperateType.UPDATE)
    @PutMapping(value = RestApiConfig.UPDATE)
    @PreAuthorize("@pms.hasPermission('sys_post_edit')")
    public R<?> updateById(@RequestBody SysPost sysPost) {
        return R.success(sysPostService.updateById(sysPost));
    }

    /**
     * 通过id删除岗位信息表
     *
     * @param postId id
     * @return R
     */
    @Operation(summary = "通过id删除岗位信息表", description = "通过id删除岗位信息表")
    @LogAnnotation(title = "删除岗位", operateType = OperateType.DELETE)
    @DeleteMapping(value = RestApiConfig.DELETE)
    @PreAuthorize("@pms.hasPermission('sys_post_del')")
    public R<?> delete(@RequestBody Long[] postIds) {
        return R.success(sysPostService.removeBatchByIds(Arrays.asList(postIds)));
    }

    /**
     * 通过id查询岗位信息表
     *
     * @param postId id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping(RestApiConfig.GET + "/{postId}")
    @PreAuthorize("@pms.hasPermission('sys_post_view')")
    public R<?> findById(@PathVariable("postId") Long postId) {
        return R.success(sysPostService.getById(postId));
    }

    /**
     * 通过条件查询岗位信息表
     *
     * @param queryPost
     * @return R
     */
    @Operation(summary = "通过条件查询", description = "通过条件查询")
    @GetMapping(RestApiConfig.DETAILS)
    @PreAuthorize("@pms.hasPermission('sys_post_view')")
    public R<?> details(SysPost queryPost) {
        return R.success(sysPostService.getOne(queryPost));
    }

    /**
     * 获取岗位列表
     *
     * @return 岗位列表
     */
    @GetMapping(RestApiConfig.ALL)
    @PreAuthorize("@pms.hasPermission('sys_post_view')")
    public R<List<SysPost>> findAll() {
        return R.success(sysPostService.findAll());
    }

    /**
     * 获取岗位列表
     *
     * @return 岗位列表
     */
    @GetMapping(RestApiConfig.LIST)
    @PreAuthorize("@pms.hasPermission('sys_post_view')")
    public R<List<SysPost>> findList(SysPost sysPost) {
        return R.success(sysPostService.findList(sysPost));
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping(RestApiConfig.PAGE)
    @PreAuthorize("@pms.hasPermission('sys_post_view')")
    public R<?> getPage(Page<SysPost> page, SysPost sysPost) {
        return R.success(sysPostService.findPage(page, sysPost));
    }


    /**
     * 导入
     */
    @PostMapping(RestApiConfig.IMPORT_EXCEL)
    @PreAuthorize("@pms.hasPermission('sys_post_import')")
    public R<?> importExcel(@RequestExcel List<PostExcelVO> postExcelVOList, BindingResult bindingResult) {
        return sysPostService.importExcel(postExcelVOList, bindingResult);
    }

    /**
     * 导出
     */
    @ResponseExcel
    @GetMapping(RestApiConfig.EXPORT_EXCEL)
    @PreAuthorize("@pms.hasPermission('sys_post_export')")
    public List<PostExcelVO> exportExcel() {
        return sysPostService.listPost();
    }
}
